package com.duke.shaking.interaction;

import java.util.ArrayList;
import java.util.List;


import com.duke.shaking.pojo.ChatFriend;
import com.duke.shaking.storage.DataManager;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ChatFriendsListView extends ListView 
						implements OnItemClickListener{

	private List<ChatFriend> friends;
	private ChatFriendsListAdatper adapter;
	private ChatFriendSelectListener selectListener;
	
	public ChatFriendsListView(Context context, AttributeSet attrs) {
		super(context, attrs);

		friends = loadMessagesFriends();
		adapter = new ChatFriendsListAdatper(this.getContext(), friends);
		setAdapter(adapter);
		setOnItemClickListener(this);
	}
	
	public ChatFriend getFriendByPosition(int position) {
		if (position >= 0 && position < friends.size()) {
			return friends.get(position);
		}
		return null;
	}
	
	public List<ChatFriend> getFriends() {
		return friends;
	}
	
	public void removeItemAtPosition (int position) {
		if (position >= 0 && position < friends.size()) {
			ChatFriend f = friends.get(position);
			DataManager dm = DataManager.getShared();
			dm.removeAllMessagesOfFriendByUserId(f.getUserId());
			dm.removeFriendByUserId(f.getUserId());
			friends.remove(position);
			adapter.notifyDataSetChanged();
		}
	}
	
	public void clearMessageOfFriend(int position) {
		if (position >= 0 && position < friends.size()) {
			ChatFriend f = friends.get(position);
			DataManager dm = DataManager.getShared();
			dm.removeAllMessagesOfFriendByUserId(f.getUserId());
			f.setUnreadMessages(0);
			f.setTotalMessages(0);
			adapter.notifyDataSetChanged();
		}
	}
	
	public void removeAllItems() {
		DataManager dm = DataManager.getShared();
		dm.removeAllMessages();
		dm.removeAllFriends();
		friends.clear();
		adapter.notifyDataSetChanged();
	}
	
	private List<ChatFriend> loadMessagesFriends() {
		
		/*friends = new LinkedList<ChatFriend>();
		
		for (int i = 0; i < 10; i ++) {
			ChatFriend f = new ChatFriend();
			// for test data:
	     	// 18612183721: 224862
	     	// 18611989506: 224861
	        f.setUserId("224861"); // for test
			f.setNickname("name" + i);
			friends.add(f);
		}
		*/
		
		List<ChatFriend> fs = DataManager.getShared().loadFriends();
		if (fs == null) {
			fs = new ArrayList<ChatFriend>();
		}
		return fs;
	}

	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Log.i("shaking", "chat friend list onItemClick: " + position);
		if (position < 0 || position >= friends.size()) {
			return;
		}
		ChatFriend friend = friends.get(position);
		if (selectListener != null && friend != null) {
			selectListener.onSelected(friend);
		}
	}

	public void setSelectListener(ChatFriendSelectListener selectListener) {
		this.selectListener = selectListener;
	}
	
	public void reloadData() {
		List<ChatFriend> fs = loadMessagesFriends();
		if (fs != null) {
			friends.clear();
			friends.addAll(fs);
		}
		else {
			friends.clear();
		}
		adapter.notifyDataSetChanged();
	}

}
